package com.chatmcp.mcprouter.cli;

import org.springframework.boot.ApplicationArguments;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;

import java.util.Arrays;

/**
 * 代理服务器命令
 * 用于启动MCP代理服务器
 */
public class ProxyCommand implements Command {

    @Override
    public void execute(ApplicationContext context, String[] args, ApplicationArguments applicationArgs) throws Exception {
        System.out.println("启动MCP代理服务器...");
        
        // 设置服务器类型为proxy
        System.setProperty("mcp.server.type", "proxy");
        
        // 获取配置文件
        String configFile = ".env.properties";
        if (applicationArgs.containsOption("c") || applicationArgs.containsOption("config")) {
            String[] configValues = applicationArgs.containsOption("c") 
                ? applicationArgs.getOptionValues("c").toArray(new String[0])
                : applicationArgs.getOptionValues("config").toArray(new String[0]);
            
            if (configValues.length > 0) {
                configFile = configValues[0];
                System.out.println("使用配置文件: " + configFile);
                System.setProperty("spring.config.location", "classpath:/application.properties," + configFile);
            }
        }
        
        // 获取端口
        String port = "8025"; // 默认端口
        Environment env = context.getEnvironment();
        String configuredPort = env.getProperty("proxy_server.port");
        if (configuredPort != null && !configuredPort.isEmpty()) {
            port = configuredPort;
        }
        System.out.println("代理服务器监听端口: " + port);
        System.setProperty("server.port", port);
    }

    @Override
    public String getDescription() {
        return "启动MCP代理服务器";
    }

    @Override
    public String getUsage() {
        return "proxy [-c|--config 配置文件路径]";
    }
} 